package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzq.domain.dto.SmileClientDTO;
import com.zzq.domain.entity.SmileClient;
import com.zzq.domain.search.ClientSearchParamet;
import com.zzq.enumerate.RedisKeyEnum;
import com.zzq.exception.NormalException;
import com.zzq.mapper.SmileClientMapper;
import com.zzq.service.SmileClientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzq.util.ObjectUtils;
import com.zzq.util.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 谢文宗
 * @since 2023-08-22
 */
@Service
public class SmileClientServiceImpl extends ServiceImpl<SmileClientMapper, SmileClient> implements SmileClientService {

    @Autowired
    SmileClientMapper smileClientMapper;

    @Autowired
    RedisCache redisCache;

    @Override
    public String getClientNo() {
        LambdaQueryWrapper<SmileClient> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SmileClient::getCreatedTime);
        IPage<SmileClient> page=new Page(1,1);
        IPage<SmileClient> iPage = smileClientMapper.selectPage(page, wrapper);
        List<SmileClient> records = iPage.getRecords();
        if (ObjectUtils.isEmpty(records)){
            return "1001";
        }
        SmileClient smileClient = records.get(0);
        String clientNo = smileClient.getClientNo();
        Integer i = Integer.parseInt(clientNo);
        String no = i + 1+"";
        return no;
    }

    @Override
    public List<SmileClientDTO> getSmileClientDTOList(ClientSearchParamet searchParamet) {
        return  smileClientMapper.getSmileClientDTOList(searchParamet);
    }

    @Override
    public Integer setInfo(SmileClient smileClient) {

        int i = smileClientMapper.updateById(smileClient);
        redisCache.setCacheObject(RedisKeyEnum.SMILE_CLIENT_LOGIN.key(smileClient.getId()), smileClient);
          return i;
    }

    @Override
    public SmileClient getClientInfoByClientNo(String clientNo) {
    LambdaQueryWrapper<SmileClient> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(SmileClient::getClientNo,clientNo);
        if (!ObjectUtils.isEmpty(clientNo)) {
          return   smileClientMapper.selectOne(queryWrapper);
        }else {
            throw new NormalException("查无此人");
        }

    }


}
